package cn.itcast.service.system.impl;

import cn.hutool.core.util.IdUtil;
import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RoleServiceImpl implements RoleService {

    //依赖注入
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll(String companyId) {
        return roleDao.findAll(companyId);
    }

    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public void save(Role role) {
        //设置主键
        role.setId(IdUtil.simpleUUID());
        roleDao.save(role);
    }

    @Override
    public void update(Role role) {
        roleDao.update(role);
    }

    @Override
    public void delete(String id) {
        roleDao.delete(id);
    }

    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize, String companyId) {
        //开启分页插件
        PageHelper.startPage(pageNum,pageSize);
        //调用方法
        List<Role> list = roleDao.findAll(companyId);
        //封装分页对象
        return new PageInfo(list);
    }

    @Override
    public List<String> findModuleIdsByRoleId(String roleId) {
        //调用方法

        return roleDao.findModuleIdsByRoleId(roleId);
    }


    //角色分配
    @Override
    public void updateRoleModule(String roleId, String[] moduleIds) {
        //根据角色id删除中间表中的数据
        roleDao.deleteModuleIdsByRoleId(roleId);

        //遍历模块数组,向中间表中添加数据
        if (moduleIds!=null && moduleIds.length>0){
            for (String moduleId : moduleIds) {
                //调用方法
                roleDao.saveRoleIdAndModuleId(roleId,moduleId);
            }
        }
    }
}
